#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ----------------------------------------------------------
# -- This's Dispose Java Class
# -- 处理 Java 类（拼装实体）
# ****************************
# Author: lmay.Zhou
# Blog: www.lmaye.com
# Date: 2018年3月8日 16:37:42
# ----------------------------------------------------------
import re
from core.utils import string_utils


def get_top(class_name, class_cn):
    """
        获取实体类顶部

        :param str class_name:  类名
        :param object class_cn: 类配置
        :return:                实体类顶部内容
    """
    # 包名
    package_name = class_cn["package_name"]
    # 包路径
    package = class_cn["package_url"] % package_name
    # 实体类描述
    class_desc = class_cn["class_desc"]
    # 基础引入类
    it_class = ""
    for item in class_cn["import_class"]:
        it_class += item
    # 所有引入类
    import_class = it_class
    # 如果注解包含字符替换
    if re.search("%s", it_class):
        import_class = import_class % class_cn["extends_class"]
    import_class += "\n"
    # 类注释
    annotation = "/**\n" \
                 " * " + class_name + "\n" \
                 " * -- " + class_desc + "\n" \
                 " *\n" \
                 " * @author " + class_cn["author"] + "\n" \
                 " * @date " + class_cn["date"] + "\n" \
                 " * @qq " + str(class_cn["qq"]) + "\n" \
                 " * @email " + class_cn["email"] + "\n" \
                 " */\n"
    # 实体类注解
    class_annotation = ""
    for item in class_cn["class_annotation"]:
        class_annotation += item
    # 如果注解包含字符替换
    if re.search("%s", class_annotation):
        class_annotation = class_annotation % class_desc
    return package + import_class + annotation + class_annotation


def get_body(class_name, class_cn, input_file):
    """
        获取实体类主体

        :param str class_name:  类名
        :param object class_cn: 类配置
        :param file input_file: 读取文件
        :return:                实体类主题
    """
    # 继承模式
    extends_mode = class_cn["extends_mode"] + " " if class_cn["extends_mode"] else ""
    # 继承类
    extends_class = class_cn["extends_class"] + " " if class_cn["extends_class"] else ""
    body = "public class " + class_name + " " + extends_mode + extends_class + class_cn["implement_mode"] + " "
    # 遍历实现类
    for item in class_cn["implement_class"]:
        body += item + ", "
    # 去除最后一次拼接的符号
    body = body[:-2]
    # 拼接花括号(左半部分)
    body += " {\n"
    # 遍历初始常量
    for constant in class_cn["constants"]:
        body += constant
    body += "\n"
    # 打开文件
    f = open(input_file, 'r', encoding='UTF-8')
    cn = len(f.readlines())
    f.close()
    rf = open(input_file, 'r', encoding='UTF-8')
    r = 0
    for line in rf:
        r += 1
        # 字段正则
        p1 = re.compile(r"[a-z_]+", re.I)
        column = p1.match(line).group(0)
        # 字段驼峰式命名
        column_is_use_camelcase = class_cn["column_is_use_camelcase"]
        if column_is_use_camelcase:
            column = string_utils.column_camelcase_generate(column)
        # 字段描述正则
        p2 = re.compile("[^a-zA-Z_\s][\u4e00-\u9fa5\d\s\S]+[a-zA-Z_\s\S]*[^\n]")
        desc = p2.search(line).group(0)
        column_desc = "\t/**\n\t * " + desc + "\n\t */\n"
        # TODO 【字段类型】暂未做处理
        column_definition = "\tprivate String " + column
        # 字段注解
        column_annotation = ""
        for annotation in class_cn["column_annotation"]:
            column_annotation += annotation
        # 如果注解包含字符替换
        if re.search("%s", column_annotation):
            column_annotation = column_annotation % desc
        # 校验必填正则
        p3 = re.compile("[\u5fc5][\u586b]")
        if p3.search(line) and class_cn["is_verify"]:
            # [简短]字段描述正则
            ds = re.search("[\u4e00-\u9fa5]+", desc).group(0)
            # 字段非公用的注解
            column_not_public_annotation = ""
            for not_public_annotation in class_cn["column_not_public_annotation"]:
                column_not_public_annotation += not_public_annotation
            body += column_desc
            # 如果注解包含字符替换
            if re.search("%s", column_not_public_annotation):
                column_not_public_annotation = column_not_public_annotation % ds
            body += column_not_public_annotation + column_annotation + column_definition + ";\n"
        else:
            body += column_desc + column_annotation + column_definition + ";\n"
        if cn != r:
            body += "\n"
    body += "}"
    rf.close()
    return body


def generate_enum_constant(input_file):
    """
        生成枚举常量

        :param file input_file: 读取文件
        :return:                枚举常量
    """
    f = open(input_file, 'r', encoding='UTF-8')
    content = ""
    for line in f:
        p1 = re.compile(r"[a-z_\d]+", re.I)
        column = p1.match(line).group(0)
        column_upper = column.upper()
        p2 = re.compile("[^a-zA-Z_\s][\u4e00-\u9fa5\d\s\S]+[a-zA-Z_\s\S]*[^\n]")
        desc = p2.search(line).group(0)
        content += column_upper + "(\"" + column + "\", \"" + desc + "\"),\n"
    f.close()
    return content
